<?php
include_once'../init.inc.php';
include_once 'header.php';

?>
<script>
 $(document).ready(function(){
     user_tbs = $.cookie("user_tbs")!==null?decodeURI($.cookie("user_tbs")):""; 
     pwd_tbs = $.cookie("pwd_tbs")!==null?decodeURI($.cookie("pwd_tbs")):"";  
     $("#tbsusr").val(user_tbs);
     $("#tbspwd").val(pwd_tbs);
 });
</script>

     <div id="tbs">
        <form name="everyones" method = "post">
        user:<br>
                <input id="tbsusr" type="text" name="user" value="" class="texttbs"><br>
        password:<br>
                <input id="tbspwd" type="password" name="pass" value="" class="texttbs"><br>

        <h2> Replace everyones favorites </h2><br>
        text:<br>
                <textarea name="textfavor" class="areatbs" rows="50" cols="50"><?=@$_REQUEST['textfavor']?></textarea><br>
        max syns:<br>
                <input type="text" name="maxsyns" value="<?=@$_REQUEST['maxsyns']?>" class="texttbs" ><br>
        qualiy:<br>
                <select name="quality" class="texttbs">
                    <option selected value="1">Good</option>
                    <option value="2">Better</option>
                    <option value="3">Best</option>
                </select><br>
        protected terms(comma delimited list of words and phrases you don't want replaced):<br>
                <input type="text" name="protectever" value="<?=@$_REQUEST['protectever']?>" class="texttbs"><br>
        output:<br>
                <textarea name="outputever" id ="output1" class="areatbs" rows="50" cols="50"><?=@$_REQUEST['outputever']?></textarea><br>
        <input id="everyones" type="submit" name="everyones" value="replace everyones favorites" class="btn btn-primary"> 



        <h2>Rewrite text</h2><br>
        text:<br>
                <textarea name="rewritetext" class="areatbs" rows="50" cols="50"><?=@$_REQUEST['rewritetext']?></textarea><br>
        protected terms(comma delimited list of words and phrases you don't want replaced):<br>
                <input type="text" name="protectrew" value="<?=@$_REQUEST['protectrew']?>" class="texttbs"><br>
        output:<br>
                <textarea id="output2" name="outputrew" class="areatbs" rows="50" cols="50"><?=@$_REQUEST['outputrew']?></textarea><br>
        <input id="rewrite" type="submit" name="rewrite" value="rewrite text" class="btn btn-primary">    
        </form>
    </div>

<?php

/*00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*/
if (@$_REQUEST['everyones']){
    $testmethod="replaceEveryonesFavorites";
    $username = $_REQUEST['user'];
    $password = $_REQUEST['pass'];
    $text = $_REQUEST['textfavor'];
    $protectedterms = $_REQUEST['protectever'];
    $quality = (int)$_REQUEST['quality'];
    $maxsyns = (int)$_REQUEST['maxsyns'];
    
    $result = json_encode(getTbs($testmethod ,$username, $password, $text, $protectedterms, $maxsyns, $quality));

    echo "<script>
        $('#output1').text($result);
        </script>";
    
}
if (@$_REQUEST['rewrite']){
      $testmethod="rewriteText";
      $username = $_REQUEST['user'];
      $password = $_REQUEST['pass'];
      $text = $_REQUEST['rewritetext'];
      $protectedterms = $_REQUEST['protectrew'];
    
    
    $result = json_encode(getTbs($testmethod ,$username, $password, $text, $protectedterms, $maxsyns, $quality));
    
    
    echo "<script>
        $('#output2').val($result);
        </script>";
}

/*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*/
function curl_post($url, $data, &$info){

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, curl_postData($data));
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_REFERER, $url);
  $html = trim(curl_exec($ch));
  curl_close($ch);

  return $html;
}

function curl_postData($data){

  $fdata = "";
  foreach($data as $key => $val){
    $fdata .= "$key=" . urlencode($val) . "&";
  }

  return $fdata;

}

function getTbs($testmethod ,$username, $password, $text, $protectedterms, $maxsyns, $quality=1){

$url = 'http://thebestspinner.com/api.php';

#$testmethod = 'identifySynonyms';
//$testmethod = 'replaceEveryonesFavorites';


# Build the data array for authenticating.

$data = array();
$data['action'] = 'authenticate';
$data['format'] = 'php'; # You can also specify 'xml' as the format.

# The user credentials should change for each UAW user with a TBS account.

//$data['username'] = 'your@useremail.com';
//$data['password'] = 'password';
$data['username'] = $username;
$data['password'] = $password;
# Authenticate and get back the session id.
# You only need to authenticate once per session.
# A session is good for 24 hours.
$output = unserialize(curl_post($url, $data, $info));

if($output['success']=='true'){
  # Success.
  $session = $output['session'];
  
  # Build the data array for the example.
  $data = array();
  $data['session'] = $session;
  $data['format'] = 'php'; # You can also specify 'xml' as the format.
  //$data['text'] = 'Losing weight can be a difficult thing to do.';
  $data['text'] = $text;
  $data['action'] = $testmethod;
  $data['protectedterms'] = $protectedterms;
  //$data['maxsyns'] = '3'; # The number of synonyms per term.
  
  if($testmethod=='replaceEveryonesFavorites'){
    # Add a quality score for this method.
    $data['quality'] = $quality;
    $data['maxsyns'] = $maxsyns; # The number of synonyms per term.
  }

  # Post to API and get back results.
  $output = curl_post($url, $data, $info);
  $output = unserialize($output);
  
  # Show results.
  //echo "<p><b>Method:</b><br>$testmethod</p>";
  //echo "<p><b>Text:</b><br>$data[text]</p>";
  
  $data['action'] = 'apiQuota';
  $quota = curl_post($url, $data, $info);
  $quota = unserialize($quota);
  
  if($output['success']=='true'){
      
        
             //echo "<p><b>Output:</b><br>" . str_replace("\r", "<br>", $output['output']) . "</p><p>Remaining quota: $quota[output]</p>";
                return  str_replace("\r", "<br>", $output['output']);
       
        
                
         
  }
  else{
      
    echo "<p><b>Error:</b><br>$output[error]</p>";
  }
}
else{
  # There were errors.
  //echo "<p><b>Error:</b><br>$output[error]</p>";
  return $output['error'];
}

}
/*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*/

 include_once 'footer.php'; 
?>